package com.zky.order.service.impl;

import com.zky.order.domain.OrderPetAcquisition;
import com.zky.order.dto.OrderDto;
import com.zky.order.mapper.OrderPetAcquisitionMapper;
import com.zky.order.query.OrderPetAcquisitionQuery;
import com.zky.order.service.IOrderPetAcquisitionService;
import com.zky.basic.service.impl.BaseServiceImpl;
import com.zky.org.domain.Employee;
import com.zky.org.domain.Shop;
import com.zky.org.mapper.EmployeeMapper;
import com.zky.org.mapper.ShopMapper;
import com.zky.pet.domain.Pet;
import com.zky.pet.domain.SearchMasterMsg;
import com.zky.pet.mapper.PetMapper;
import com.zky.pet.mapper.SearchMasterMsgMapper;
import com.zky.user.domain.Logininfo;
import com.zky.util.LoginContext;
import com.zky.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<OrderPetAcquisition> pageQuery(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLongininfoId(logininfo.getId());
        //判断是不是平台管理员
        if (employee.getShop_id()!= null){
            query.setShopId(employee.getShop_id());
            Shop shop = shopMapper.findOne(employee.getShop_id());
            //判断是不是店长
            if (!shop.getAdmin().equals(employee.getId())){
                query.setEmpId(employee.getId());
            }
        }
        return super.pageQuery(query);
    }

    @Override
    public void cancel(Long id) {
        //将寻主消息放入寻主池，state= 3，shopId = null
        //查找当前订单
        OrderPetAcquisition order = orderPetAcquisitionMapper.findOne(id);
        //将当前订单放入寻主池
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        //修改订单状态为-1
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
    }

    @Override
    public void confirm(OrderDto orderDto) {
        //订单确认：确认以后平台就会多一条宠物信息
        OrderPetAcquisition order = orderPetAcquisitionMapper.findOne(orderDto.getId());
        //获取订单信息
        SearchMasterMsg msg = searchMasterMsgMapper.findOne(order.getSearchMasterMsgId());
        Pet pet = msg2Pet(msg,order);
        petMapper.add(pet);

        //2.修改订单状态为1 价格，支付方式
        order.setState(1);
        order.setPrice(orderDto.getMoney());
        order.setPaytype(orderDto.getPayType());
        order.setPetId(orderDto.getId());
        orderPetAcquisitionMapper.update(order);

        //3.修改寻主消息状态
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
    }

    private Pet msg2Pet(SearchMasterMsg msg, OrderPetAcquisition order) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(order.getPrice());
        pet.setSaleprice(order.getPrice());
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
        pet.setUserId(msg.getUserId());
        pet.setSearchMasterMsgId(msg.getId());


        return pet;
    }
}
